#ifndef __KEYEVENTS_H  
#define __KEYEVENTS_H  

	static int curAction = 0;

	public static int lastKeyCode 	= 0;
	public static boolean newEvent 	= false;

	protected void keyPressed(int keyCode)
	{
		newEvent = true;
		keyCode = ABS(keyCode);
		lastKeyCode = keyCode;
		
		if (KEYS.SET[KEYS.IDX_LSK] 			== keyCode) keyflags |= KEYS.F_KEY_LSK;
		else if (KEYS.SET[KEYS.IDX_RSK] 	== keyCode) keyflags |= KEYS.F_KEY_RSK;
		else if (KEYS.SET[KEYS.IDX_UP] 		== keyCode) keyflags |= KEYS.F_KEY_UP;
		else if (KEYS.SET[KEYS.IDX_DOWN] 	== keyCode) keyflags |= KEYS.F_KEY_DOWN;
		else if (KEYS.SET[KEYS.IDX_LEFT] 	== keyCode) keyflags |= KEYS.F_KEY_LEFT;
		else if (KEYS.SET[KEYS.IDX_RIGHT] 	== keyCode) keyflags |= KEYS.F_KEY_RIGHT;
		else if (KEYS.SET[KEYS.IDX_FIRE] 	== keyCode) keyflags |= KEYS.F_KEY_FIRE;

		curAction = getActionFromKeyCode(keyCode);
	}
	
	protected void keyReleased(int keyCode)
	{
		keyCode = ABS(keyCode);
		
		if (KEYS.SET[KEYS.IDX_LSK] 			== keyCode) keyflags &=~ KEYS.F_KEY_LSK;
		else if (KEYS.SET[KEYS.IDX_RSK] 	== keyCode) keyflags &=~ KEYS.F_KEY_RSK;
		else if (KEYS.SET[KEYS.IDX_UP] 		== keyCode) keyflags &=~ KEYS.F_KEY_UP;
		else if (KEYS.SET[KEYS.IDX_DOWN] 	== keyCode) keyflags &=~ KEYS.F_KEY_DOWN;
		else if (KEYS.SET[KEYS.IDX_LEFT] 	== keyCode) keyflags &=~ KEYS.F_KEY_LEFT;
		else if (KEYS.SET[KEYS.IDX_RIGHT] 	== keyCode) keyflags &=~ KEYS.F_KEY_RIGHT;
		else if (KEYS.SET[KEYS.IDX_FIRE] 	== keyCode) keyflags &=~ KEYS.F_KEY_FIRE;
		
		curAction = ACTIONS.RELEASED_LAST;
	}  
	
	public static boolean isKeyPressed(int keyCode)
	{
		if (KEYS.SET[KEYS.IDX_LSK] == keyCode)
			if((keyflags&KEYS.F_KEY_LSK)!=0)
				return true;
		if (KEYS.SET[KEYS.IDX_RSK] == keyCode)
			if((keyflags&KEYS.F_KEY_RSK)!=0)
				return true;
		if (KEYS.SET[KEYS.IDX_UP] == keyCode)
			if((keyflags&KEYS.F_KEY_UP)!=0)
				return true;
		if (KEYS.SET[KEYS.IDX_DOWN] == keyCode)
			if((keyflags&KEYS.F_KEY_DOWN)!=0)
				return true;
		if (KEYS.SET[KEYS.IDX_LEFT] == keyCode)
			if((keyflags&KEYS.F_KEY_LEFT)!=0)
				return true;
		if (KEYS.SET[KEYS.IDX_RIGHT] == keyCode)
			if((keyflags&KEYS.F_KEY_RIGHT)!=0)
				return true;
		if (KEYS.SET[KEYS.IDX_FIRE] == keyCode)
			if((keyflags&KEYS.F_KEY_FIRE)!=0)
				return true;
				
		return false;
	}
	
	public int getActionFromKeyCode(int keyCode) {
		if (KEYS.SET[KEYS.IDX_LSK] == keyCode)
			if((keyflags&KEYS.F_KEY_LSK)!=0)
				return ACTIONS.BACK;
		if (KEYS.SET[KEYS.IDX_RSK] == keyCode)
			if((keyflags&KEYS.F_KEY_RSK)!=0)
				return ACTIONS.NEXT;
		if (KEYS.SET[KEYS.IDX_UP] == keyCode)
			if((keyflags&KEYS.F_KEY_UP)!=0)
				return ACTIONS.UP;
		if (KEYS.SET[KEYS.IDX_DOWN] == keyCode)
			if((keyflags&KEYS.F_KEY_DOWN)!=0)
				return ACTIONS.DOWN;
		if (KEYS.SET[KEYS.IDX_LEFT] == keyCode)
			if((keyflags&KEYS.F_KEY_LEFT)!=0)
				return ACTIONS.LEFT;
		if (KEYS.SET[KEYS.IDX_RIGHT] == keyCode)
			if((keyflags&KEYS.F_KEY_RIGHT)!=0)
				return ACTIONS.RIGHT;
		if (KEYS.SET[KEYS.IDX_FIRE] == keyCode)
			if((keyflags&KEYS.F_KEY_FIRE)!=0)
				return ACTIONS.SELECT;
		return 0;
	}
	
	public static void clearAction(boolean forceClear)
	{
		if(curAction == ACTIONS.RELEASED_LAST || forceClear)
		{
			curAction = ACTIONS.NONE;
		}
	}
	
	public void clearKeys()
	{
		//clearAction();
		lastKeyCode = 0;		
		keyflags = 0;
	}
  
#endif // __KEYEVENTS_H  
